package com.zonerobotics.zBotAPI.ZBot;

/**
 * This simply holds stats related to the ZBot
 * @author Michael McCarty
 * Apr 8, 2013
 */
public class ZBotStats {
	private int RXBytes;
	private int TXBytes;
	private int rxadds = 0;
	private int txadds = 0;
	private double[] timeStamps = {System.nanoTime(),System.nanoTime(),System.nanoTime()};
	private double RXBps = 0;
	private double TXBps = 0;
	private double RXBpsTotal = 0;
	private double TXBpsTotal = 0;
	private int arraySize = 25;
	private double[] RXBpsRunningTotal = new double[arraySize];
	private double[] TXBpsRunningTotal = new double[arraySize];
	private int RXIndex = 0;
	private int TXIndex = 0;
	final double NANOS_PER_SECOND = 1000000000.0;
	final double BYTES_PER_MIB = 1024 * 1024;
	double[] previous = {0,0};
	static final double ALPHA = 1 - 1.0/6;
	
	public int getRXBytes() {
		return RXBytes;
	}
	public void setRXBytes(int rXBytes) 
	{
		RXBytes = rXBytes;
	}
	
	/**
	 * this will add the bytes to the current total and update the bps
	 * @param rXBytes
	 */
	public void addRXbytes(int rXBytes)
	{
		rxadds++;
		double d = NANOS_PER_SECOND*(rXBytes/(System.nanoTime() - timeStamps[1]+1));
		RXBpsTotal = RXBpsTotal - RXBpsRunningTotal[RXIndex] + d;
		RXBpsRunningTotal[RXIndex] = d;
		RXIndex = (RXIndex+1)%arraySize;
		RXBps = RXBpsTotal/arraySize;
		timeStamps[1] = System.nanoTime();
		RXBytes += rXBytes;
	}
	
	/**
	 * this allows caller to send in the bytes and the time elapsed
	 * @param bytes
	 * @param nanos
	 */
	public void addNextRXBps(int bytes, double nanos)
	{
		double d = NANOS_PER_SECOND*(bytes/nanos);
		RXBpsTotal = RXBpsTotal - RXBpsRunningTotal[RXIndex] + d;
		RXBpsRunningTotal[RXIndex] = d;
		RXIndex = (RXIndex+1)%arraySize;
		RXBps = RXBpsTotal/arraySize;
		RXBytes += bytes;
	}
	
	/** 
	 * this will update the txbytes and calculate the next txbytes speed
	 * based off the bytes and the time passed in bytes/delta ns
	 * @param bytes
	 * @param nanos
	 */
	public void addNextTXBps(int bytes, double nanos)
	{
		txadds++;
		double d = NANOS_PER_SECOND*(bytes/nanos);
		TXBpsTotal = TXBpsTotal - TXBpsRunningTotal[TXIndex] + d;
		TXBpsRunningTotal[TXIndex] = d;
		TXIndex = (TXIndex+1)%arraySize;
		TXBps = TXBpsTotal/arraySize;
		timeStamps[0] = System.nanoTime();
		TXBytes += bytes;
	}
	
	public int getTXBytes() {
		return TXBytes;
	}
	public void setTXBytes(int tXBytes) {
		TXBytes = tXBytes;
	}
	/**
	 * this will add the bytes to the current total and update the bps
	 * @param tXBytes
	 */
	public void addTXBytes(int tXBytes)
	{
		txadds++;
		double d = NANOS_PER_SECOND*(tXBytes/(System.nanoTime() - timeStamps[0]+1));
		TXBpsTotal = TXBpsTotal - TXBpsRunningTotal[TXIndex] + d;
		TXBpsRunningTotal[TXIndex] = d;
		TXIndex = (TXIndex+1)%arraySize;
		TXBps = TXBpsTotal/arraySize;
		timeStamps[0] = System.nanoTime();
		TXBytes += tXBytes;
	}
	
	public double getRXBps() {
		return RXBps;
	}
	public double getRXbps() {
		return RXBps*8;
	}
	public void setRXBps(double rXbps) {
		RXBps = rXbps;
	}
	public double getTXBps() {
		return TXBps;
	}
	public double getTXbps() {
		return TXBps*8;
	}
	public void setTXBps(double tXbps) {
		TXBps = tXbps;
	}
	

}
